package com.cdream.basic.thread;

import java.util.concurrent.locks.LockSupport;

/**
 * @author cdream
 * @date 2021/9/9
 */
public class LockSupportExample {
    private static Thread t1,t2;
    public static void main(String[] args) {

        // extracted();

        Thread t1 = new Thread(()-> {
            LockSupport.park();
            System.out.println("dog");
        });
        LockSupport.unpark(t1);
        t1.start();
    }

    private static void extracted() {
        char[] char1 = "123456".toCharArray();
        char[] char2 = "abcdef".toCharArray();
        t1 = new Thread(()-> {
            for (char c : char1) {
                System.out.println(c);
                System.out.println("unpark2 start");
                LockSupport.unpark(t2);
                System.out.println("unpark2 end");
                System.out.println("park1 start");
                LockSupport.park();
                System.out.println("park1 end");
            }
        });
        t2 = new Thread(()-> {
            for (char c : char2) {
                System.out.println("park2 start");
                LockSupport.park();
                System.out.println("park2 end");
                System.out.println(c);
                System.out.println("unpark1 start");
                LockSupport.unpark(t1);
                System.out.println("unpark1 end");
            }
        });
        t1.start();
        t2.start();
    }
}
